//
//  BSZCodeViewController.m
//  BuSuanZi
//
//  Created by staff on 3/15/25.
//

#import "BSZLoginCodeViewController.h"

#define TIMECOUNT      59

static const NSString *cellIdentifer = @"BSZLoginCodeViewController";


@interface BSZLoginCodeViewController ()<UITextViewDelegate>

@property (nonatomic, strong) UITextField *phoneTextFiled;
@property (nonatomic, strong) UITextField *codeTextFiled;
@property (nonatomic, strong) UIButton    *codeButton;
@property (nonatomic, strong) UIButton    *agreeButton;

@end

@implementation BSZLoginCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 初始化
    [self stepTableView];
}

//键盘消失
- (void)dismissKeyBoardAction{
    [self.phoneTextFiled resignFirstResponder];
    [self.codeTextFiled resignFirstResponder];
}

//初始化列表
- (void)stepTableView{
    self.dataTableView.separatorColor = ClearColor;
    self.dataTableView.backgroundColor = WhiteColor;
    self.dataTableView.bounces = YES;
    self.dataTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [self.view addSubview:self.dataTableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 6;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return SCREEN_WIDTH*2./3.;
    }else if (indexPath.row == 5){
        return SCREEN_WIDTH;
    }else if (indexPath.row == 3){
        return PPMUIRealValue(CELLHEIGHT);
    }else if (indexPath.row == 4){
        return PPMUIRealValue(80);
    }else{
        return PPMUIRealValue(65);
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
    if (cell) {
        [cell removeFromSuperview];
        cell = nil;
    }
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (indexPath.row == 0) {
        UILabel *signLabel = [BSZUIKit creatLabelWithFrame:CGRectMake(PPMUIRealValue(30), SCREEN_WIDTH/3., SCREEN_WIDTH-PPMUIRealValue(60), SCREEN_WIDTH/3.) text:@"验证码登录" font:SystemFont(26) textColor:DefaultBlackColor textAlignment:NSTextAlignmentLeft];
        [cell.contentView addSubview:signLabel];
    }else if (indexPath.row == 1){
        UIView *_elementView = [[UIView alloc]initWithFrame:CGRectMake(PPMUIRealValue(30), 0, SCREEN_WIDTH-PPMUIRealValue(60), PPMUIRealValue(45))];
        [_elementView setAllCorner:_elementView.height/2.0];
        _elementView.backgroundColor = ColdLightGrayColor;
        [cell.contentView addSubview:_elementView];
        
//        UILabel *prevLabel = [UILabel creatLabelWithFrame:CGRectMake(0, 0, PPMUIRealValue(CELLHEIGHT), _elementView.height) text:@"+86" font:BoldSystemFont(16) textColor:BlackColor textAlignment:NSTextAlignmentLeft];
//        [_elementView addSubview:prevLabel];
        
        BSZLayoutButton *prevButton = [BSZLayoutButton buttonWithType:UIButtonTypeCustom];
        prevButton.frame = CGRectMake(0, 0, PPMUIRealValue(60), _elementView.height);
        prevButton.imageSize = CGSizeMake(PPMUIRealValue(16), PPMUIRealValue(16));
        [prevButton setImage:[UIImage imageNamed:@"qm_down_icon"] forState:UIControlStateNormal];
        
        prevButton.midSpacing = 2;
        prevButton.titleLabel.font = SystemFont(15);
        [prevButton setTitle:@"+86" forState:UIControlStateNormal];
        [prevButton setTitleColor:DefaultBlackColor forState:UIControlStateNormal];
        prevButton.layoutStyle = BSZLayoutButtonStyleLeftTitleRightImage;
        [_elementView addSubview:prevButton];
        
        self.phoneTextFiled.frame = CGRectMake(prevButton.right+PPMUIRealValue(10), 0, _elementView.width-PPMUIRealValue(70), _elementView.height);
        [_elementView addSubview:self.phoneTextFiled];
        
        UIView *underLineView = [[UIView alloc]initWithFrame:CGRectMake(0, _elementView.height-1, _elementView.width, 1)];
        underLineView.backgroundColor = ColdLightGrayColor;
        [_elementView addSubview:underLineView];
    }else if (indexPath.row == 2){
        UIView *_elementView = [[UIView alloc]initWithFrame:CGRectMake(PPMUIRealValue(30), 0, SCREEN_WIDTH-PPMUIRealValue(60), PPMUIRealValue(45))];
        [_elementView setAllCorner:_elementView.height/2.0];
        _elementView.backgroundColor = ColdLightGrayColor;
        [cell.contentView addSubview:_elementView];
        
        self.codeTextFiled.frame = CGRectMake(0, 0, _elementView.width-PPMUIRealValue(105), _elementView.height);
        
        self.codeButton.frame = CGRectMake(_elementView.width-PPMUIRealValue(100), PPMUIRealValue(7.5), PPMUIRealValue(100), PPMUIRealValue(30));
        [_elementView addSubview:self.codeButton];
        [_elementView addSubview:self.codeTextFiled];
        
        UIView *underLineView = [[UIView alloc]initWithFrame:CGRectMake(0, _elementView.height-1, _elementView.width, 1)];
        underLineView.backgroundColor = ColdLightGrayColor;
        [_elementView addSubview:underLineView];
    }else if (indexPath.row == 3){

    }else if (indexPath.row == 4){
        UIButton *loginButton = [BSZUIKit initTextWithFrame:CGRectMake(PPMUIRealValue(30), 0, SCREEN_WIDTH-PPMUIRealValue(60), PPMUIRealValue(40)) titleForButton:@"登录" titleColorForButton:WhiteColor fontForButton:SystemFont(15.) backgroundColorForButton:DefaultColor];
        [loginButton setAllCorner:loginButton.height/2.];
        [loginButton addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:loginButton];
        
    }else {
        
        
//        self.agreeButton.frame = CGRectMake(PPMUIRealValue(30), PPMUIRealValue(200), PPMUIRealValue(20), PPMUIRealValue(20));
//        [cell.contentView addSubview:self.agreeButton];
        
        //内容文本
        NSString *content = @"注册即代表你同意《用户协议》《隐私政策》";
        CGFloat W = [[Global shareInstance] widthForText:content withFont:SystemFont(10) withHeight:PPMUIRealValue(20)]+PPMUIRealValue(20);
        UITextView *contentTextView = [[UITextView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2.-W/2., PPMUIRealValue(200), W, PPMUIRealValue(20))];
        contentTextView.backgroundColor = ClearColor;
        contentTextView.attributedText = [self getContentLabelAttributedText:content withTextArry:@[@"用户协议", @"隐私政策"] withProtocalArry:@[@"userProtocol://", @"userPolicy://"]];
        contentTextView.textAlignment = NSTextAlignmentCenter;
        contentTextView.delegate = self;
        contentTextView.editable = NO;        //必须禁止输入，否则点击将弹出输入键盘
        contentTextView.scrollEnabled = NO;
        [cell.contentView addSubview:contentTextView];
    }
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self dismissKeyBoardAction];
}

#pragma Others -- 属性
- (NSAttributedString *)getContentLabelAttributedText:(NSString *)text withTextArry:(NSArray *)textArry withProtocalArry:(NSArray *)protocalArry{
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName:SystemFont(10),NSForegroundColorAttributeName:DefaultBlackColor}];
    
    for (NSInteger i=0; i<textArry.count; i++) {
        NSString *txt = textArry[i];
        NSString *protocal = protocalArry[i];
        
        NSRange range = [text rangeOfString:txt];
        [attrStr addAttribute:NSForegroundColorAttributeName value:DefaultColor range:range];
        [attrStr addAttribute:NSLinkAttributeName value:protocal range:range];
    }
    return attrStr;
}

@end
